from django.contrib import admin
from .models import ParentBoard, Board, BoardPostPermission


class BoardInline(admin.TabularInline):
    model = Board
    extra = 1


@admin.register(ParentBoard)
class ParentBoardAdmin(admin.ModelAdmin):
    list_display = ('p_board_id', 'board_name', 'board_desc', 'require_permission', 'create_time', 'update_time')
    list_filter = ('require_permission', 'create_time')
    search_fields = ('board_name', 'board_desc')
    ordering = ('p_board_id',)
    readonly_fields = ('create_time', 'update_time')
    inlines = [BoardInline]
    list_display_links = ('p_board_id', 'board_name', 'board_desc', 'require_permission', 'create_time', 'update_time')
    
    fieldsets = (
        ('Basic Information', {
            'fields': ('board_name', 'board_desc')
        }),
        ('Permission Settings', {
            'fields': ('require_permission',),
            'description': 'After activation, users need to obtain specific permissions to post in this section'
        }),
        ('Time Information', {
            'fields': ('create_time', 'update_time')
        }),
    )


@admin.register(Board)
class BoardAdmin(admin.ModelAdmin):
    list_display = ('board_id', 'board_name', 'p_board', 'board_desc', 'create_time', 'update_time')
    list_filter = ('p_board', 'create_time')
    search_fields = ('board_name', 'board_desc')
    ordering = ('p_board', 'board_id')
    readonly_fields = ('create_time', 'update_time')
    list_display_links = ('board_id', 'board_name', 'p_board', 'board_desc', 'create_time', 'update_time')


@admin.register(BoardPostPermission)
class BoardPostPermissionAdmin(admin.ModelAdmin):
    list_display = ('id', 'user', 'p_board', 'p_board_require_permission', 'is_active', 'granted_by', 'create_time')
    list_filter = ('p_board', 'p_board__require_permission', 'is_active', 'create_time')
    search_fields = ('user__nickname', 'user__email', 'p_board__board_name')
    ordering = ('-create_time',)
    readonly_fields = ('create_time', 'update_time', 'granted_by')
    list_display_links = ('id', 'user', 'p_board', 'p_board_require_permission', 'is_active', 'granted_by', 'create_time')
    
    fieldsets = (
        ('Basic Information', {
            'fields': ('p_board', 'user', 'is_active')
        }),
        ('Permission Settings', {
            'fields': ('granted_by',),
        }),
        ('Time Information', {
            'fields': ('create_time', 'update_time')
        }),
    )
    
    def p_board_require_permission(self, obj):
        """显示父板块是否需要权限"""
        return obj.p_board.require_permission
    p_board_require_permission.short_description = 'The section requires permission'
    p_board_require_permission.boolean = True
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('p_board', 'user', 'granted_by')
    
    def save_model(self, request, obj, form, change):
        """保存时自动设置授权人"""
        obj.granted_by = request.user
        super().save_model(request, obj, form, change)